package org.anti996.lms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.anti996.lms.model.Booking;
import org.anti996.lms.model.Transport;
import org.anti996.lms.mapper.TransportMapper;
import org.anti996.lms.model.TransportOutput;
import org.anti996.lms.service.TransportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 运单 服务实现类
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */
@Service
public class TransportServiceImpl extends ServiceImpl<TransportMapper, Transport> implements TransportService {

    @Override//根据司机编号查询运单
    public List<Transport> findBydID(Integer d_id) {
        QueryWrapper<Transport> wrapper = new QueryWrapper<>();
        wrapper.eq("d_id",d_id);
        List<Transport> transports = baseMapper.selectList(wrapper);
        return transports;
    }

    @Override
    public List<TransportOutput> listAllTransports() {
        var rs = baseMapper.findAllTransports();
        for (var it : rs) {
            it.setStatus(TransportOutput.calculateStatus(it));
        }
        return rs;
    }

    @Override
    public List<TransportOutput> listAllTransportsByDriver(String username) {
        var rs = baseMapper.findAllTransportsByDriver(username);
        for (var it : rs) {
            it.setStatus(TransportOutput.calculateStatus(it));
        }
        return rs;
    }

    @Override//根据收发仓库判断未出发运单是否已存在
    public boolean isexist(Integer widfrom,Integer widto){
        QueryWrapper<Transport> wrapper = new QueryWrapper<>();
        wrapper.isNull("depart");
        wrapper.eq("w_id_from",widfrom);
        wrapper.eq("w_id_to",widto);
        return baseMapper.exists(wrapper);
    }

    @Override//根据收发仓库查询未出发运单
    public Transport findByW(Integer widfrom,Integer widto){
        QueryWrapper<Transport> wrapper = new QueryWrapper<>();
        wrapper.isNull("depart");
        wrapper.eq("w_id_from",widfrom);
        wrapper.eq("w_id_to",widto);
        return baseMapper.selectOne(wrapper);
    }

    @Override//根据司机编号查询当前运单（未出发的运单即司机的运单任务）
    public Transport findunfinish(Integer d_id) {
        QueryWrapper<Transport> wrapper = new QueryWrapper<>();
        wrapper.isNull("depart");
        wrapper.eq("d_id",d_id);
        return baseMapper.selectOne(wrapper);
    }

    @Override//根据司机编号查询未到运单（司机正在运输即将要确认到货的运单任务）
    public Transport findunarrive(Integer d_id) {
        QueryWrapper<Transport> wrapper = new QueryWrapper<>();
        wrapper.isNull("arrive");
        wrapper.isNotNull("depart");
        wrapper.eq("d_id",d_id);
        return baseMapper.selectOne(wrapper);
    }
}
